import java.util.Comparator;

class student implements Comparable<student>{
    public String name;
    public int age;
    public student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(student o) {
        if(this.name.equals( o.name)){
          return 1;
        }
        return -1;
    }

    /*
        @Override
        public int compareTo(student o) {//这里是对compareTo的重写
            if(this.age>o.age){//这里是根据年龄大小进行比较
                return 1;
            }else if(this.age<o.age){
                return -1;
            }else{
                return 0;
            }
        }

    */
    @Override
    public String toString() {
            return "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
    class AgeCompara implements Comparator<student>{
        @Override
        public int compare(student o1, student o2) {
            return o1.age-o2.age;
        }
    }
    class NameCompara implements  Comparator<student>{
        @Override
        public int compare(student o1, student o2) {
            return o1.name.compareTo(o2.name);//这里
            //返回的是不相等的字符的ASCII值。
        }
    }
public class Test1 {
    public static void main(String[] args) {
        student s1=new student("wangming",16);
        student s2=new student("ab",18);
        AgeCompara ageCompara=new AgeCompara();
        if(ageCompara.compare(s1,s2)>0){
            System.out.println("s1>s2");
        }else if(ageCompara.compare(s1,s2)<0){
            System.out.println("s1<s2");
        }else {
            System.out.println("s1=s2");
        }
        NameCompara nameCompara=new NameCompara();
        int flag= nameCompara.compare(s1,s2);
        System.out.println(flag);
        /*
        if(s1.compareTo(s2)>0){
            System.out.println("s1>s2");
        }else if(s1.compareTo(s2)<0){
            System.out.println("s1<s2");
        }else{
            System.out.println("s1=s2");
        }

         */
    }
}
